package com.cf.super_hero.controller;

import com.cf.super_hero.entity.Form;
import com.cf.super_hero.serve.impl.LoginService;
import com.cf.super_hero.tools.JWT;
import com.cf.super_hero.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class LoginControl {
    @Autowired
    LoginService heroImplementsServe;
//    登录
    @PostMapping("/login")
//    RequestBody这个注解是接收带有json参数post请求
    public Result login(@RequestBody Form form){
        log.info(form.toString());
        boolean is=heroImplementsServe.login(form);
//        如果结果数量是1,则说明有登陆过
        if(is){
            Map<String, Object> map = new HashMap<>();
            map.put("name",form.getName());
            map.put("password",form.getPassword());
            map.put("profess",form.getProfess());
            map.put("educationGrade",form.getEducationGrade());
            String jwt = JWT.getJWT(map);
            return Result.success(jwt);
        }
//        出现错误，说明在数据库中查到此人，需要先行注册
        return Result.success("error");
    }
}
